package com.chenyi.algorithm.sort;

import java.util.Arrays;

/**
 * @author chenyi
 */
public class ShellSorting {

    public static void main(String[] args) {
        int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};

        displacement(arr);

        System.out.println(Arrays.toString(arr));
    }

    /**
     * 交换法
     * @param arr
     */
    public static void exchange(int[] arr) {
        for (int group = arr.length; group > 0; group /= 2) {
            for (int i = group; i < arr.length; i++) {
                for (int j = i - group; j >= 0 ; j -= group) {
                    if (arr[j] > arr[j + group]) {
                        int temp = arr[j];
                        arr[j] = arr[j + group];
                        arr[j + group] = temp;
                    }
                }
            }
        }
    }

    /**
     * 位移法
     * @param arr
     */
    public static void displacement(int[] arr) {
        for (int group = arr.length; group > 0; group /= 2) {
            for (int i = group; i < arr.length; i++) {
                int insertIndex = i;
                int insertVal = arr[i];
                while (insertIndex - group >= 0 && insertVal < arr[insertIndex - group]) {
                    arr[insertIndex] = arr[insertIndex - group];
                    insertIndex -= group;
                }
                arr[insertIndex] = insertVal;
            }
        }
    }




}
